import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:latlong2/latlong.dart';
import '../../helper/Helper.dart';
import '../../helper/cacheHelper.dart';
import '../../helper/channelHelper.dart';
import '../../helper/global_config.dart';
import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/inventory.dart';
import '../../model/things.dart';
import '../../pages/things/inventory_info.dart';
import '../../pages/things/things_info.dart';
import '../../widgets/dialogWidget.dart';
import '../../widgets/someCards.dart';

typedef ThingsSelectCallback = void Function(Things r1);

class ThingsHelper {
  static Widget thingsCard(Things x1, VoidCallback onTap) {
    String bz = x1.TType + "  " + x1.Storage;
    return Card(
        key: Key(x1.ID.toString()),
        margin: const EdgeInsets.only(top: 0.2, bottom: 0.2),
        color: GlobalConfig.cardBackgroundColor,
        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
        child: ListTile(
          title: Text(x1.TName),
          subtitle: Text(bz),
          trailing: Text(x1.Unit),
          onTap: onTap,
        ));
  }

  static Widget thingsCard2(String TName, Unit, TType, Storage) {
    String bz = TType + "  " + Storage;
    return Card(
        key: Key(Helper.GetGuid()),
        margin: const EdgeInsets.only(top: 0.2, bottom: 0.2),
        color: GlobalConfig.cardBackgroundColor,
        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
        child: ListTile(
          title: Text(TName),
          subtitle: Text(bz),
          trailing: Text(Unit),
          // onTap: (){
          //   var x1=Things(0, TName, TType, SStyle,Unit, Storage,"");
          //   RouteHelper.ShowPage(context, ThingsInfoPage(x1));
          // }
        ));
  }

  static Widget thingsCard3(String TName, Unit, TType, Storage, double total) {
    String bz = TType + "  " + Storage;
    return Card(
        key: Key(Helper.GetGuid()),
        margin: const EdgeInsets.only(top: 0.2, bottom: 0.2),
        color: GlobalConfig.cardBackgroundColor,
        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
        child: ListTile(
          title: Text(TName),
          subtitle: Text(bz),
          trailing: Text(Helper.RoundDouble2(total, 0) + Unit),
        ));
  }

  static Widget InventoryCard2(
      BuildContext context, Inventory x1, VoidCallback added) {
    String bz = getType(x1.SType, x1.State) +
        "  " +
        Helper.RoundDouble2(x1.Amount, 0) +
        x1.Unit +
        "  " +
        x1.UserName +
        "  " +
        Helper.DateTimeFormat2(x1.CreateTM);
    return Card(
        key: Key(x1.ID.toString()),
        margin: const EdgeInsets.only(top: 0.2, bottom: 0.2),
        color: GlobalConfig.cardBackgroundColor,
        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
        child: ListTile(
          title: Text(x1.TName),
          subtitle: Text(bz),
          trailing: Text(Helper.RoundDouble2(x1.Amount2, 0)),
          onTap: () =>
              RouteHelper.ShowPage(context, InventoryInfoPage(x1, added)),
        ));
  }

  static Widget InventoryCard(
      BuildContext context, Inventory x1, VoidCallback added) {
    String bz = getType(x1.SType, x1.State) +
        "  " +
        Helper.RoundDouble2(x1.Amount, 0) +
        x1.Unit +
        "  " +
        x1.UserName +
        "  " +
        Helper.DateTimeFormat2(x1.CreateTM);
    return Card(
        key: Key(x1.ID.toString()),
        margin: const EdgeInsets.only(top: 0.2, bottom: 0.2),
        color: GlobalConfig.cardBackgroundColor,
        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
        child: ListTile(
            title: Text(x1.TName),
            subtitle: Text(bz),
            trailing: SomeCard.IfEndIcon(x1.State >= 1),
            onTap: () =>
                RouteHelper.ShowPage(context, InventoryInfoPage(x1, added)),
            onLongPress: () {
              if (x1.State > 0) {
                ChannelHelper.showToast("部门审核完成，不可删除！");
                return;
              }

              if (!Helper.UserPB(x1.UserID)) {
                ChannelHelper.showToast("只能删除本人创建！");
                return;
              }

              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return DialogWidget.getDialog(context, "删除申请", "点击确定键将删除该条单！",
                      () async {
                    x1.State = -1;
                    await Inventory.Update(x1);
                    added();
                  });
                },
              );
            }));
  }

  static String getType(int x, int s) {
    if (x == 0) return "入库单";
    if (x == 1) return "出库单";
    if (x == 2) {
      if (s == 2) {
        return "借出单-已还";
      } else {
        return "借出单";
      }
    }
    return "";
  }
}
